package br.com.checkandgo;


import br.com.checkandgo.model.Constants;
import br.com.checkandgo.model.PontosRota;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.SaveCallback;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;

public class RouteActivity extends Activity {
	
	public ProgressDialog progresso;
	public SaveCallback savePointsListener = new SaveCallback() {
		
		@Override
		public void done(ParseException e) {
			progresso.dismiss();
			Intent intent = new Intent(RouteActivity.this, MenuPrincipalActivity.class);
			startActivity(intent);
		}
	};
	
	public ParseObject rota = new ParseObject("Rota");
	
	public EditText editNome;
	public RadioButton progra;
	public RadioButton Nprogra;
	public CheckBox checkbox1;
	public CheckBox checkbox2;
	public CheckBox checkbox3;
	public CheckBox checkbox4;
	public CheckBox checkbox5;
	public CheckBox checkbox6;
	public CheckBox checkbox7;
	public EditText editHora;
	public Button btSalvar;
	
	public SaveCallback saveListener = new SaveCallback() {
		
		@Override
		public void done(ParseException e) {
			if (e == null) {
				savePoints(rota.getObjectId());
			} else {
				Log.i("error", e.getMessage().toString());
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.route);
		
		editNome = (EditText) findViewById(R.id.editText1);
		progra = (RadioButton) findViewById(R.id.radio0);
		Nprogra = (RadioButton) findViewById(R.id.radio1);
		checkbox1 = (CheckBox) findViewById(R.id.checkBox1);
		checkbox2 = (CheckBox) findViewById(R.id.checkBox2);
		checkbox3 = (CheckBox) findViewById(R.id.checkBox3);
		checkbox4 = (CheckBox) findViewById(R.id.checkBox4);
		checkbox5 = (CheckBox) findViewById(R.id.checkBox5);
		checkbox6 = (CheckBox) findViewById(R.id.checkBox6);
		checkbox7 = (CheckBox) findViewById(R.id.checkBox7);
		editHora = (EditText) findViewById(R.id.editText2);
		btSalvar = (Button) findViewById(R.id.button1);
		
		btSalvar.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				save();
			}
		
		});
		
		progresso = new ProgressDialog(this);
		progresso.setMessage("Salvando...");
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.conta, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			Intent intent = new Intent(RouteActivity.this, ContaActivity.class);
			startActivity(intent);
			return true;
		}
		if (id == R.id.home) {
			Intent intent = new Intent(RouteActivity.this, MenuPrincipalActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	public void save() {
		progresso.show();
		
		Boolean isCheckedP = progra.isChecked();
		Boolean isCheckedNP = Nprogra.isChecked();
		String nomeRota = editNome.getText().toString();
		String horaRota = editHora.getText().toString();
		
		rota.put("nome", nomeRota);
		rota.put("hora", horaRota);
		
		if(isCheckedP){
			rota.put("tipo", "Programado");
		}
		if(isCheckedNP){
			rota.put("tipo", "Nao Programado");
		}
		
		Boolean isCheckedDiaSegunda = checkbox1.isChecked();
		Boolean isCheckedDiaTerca = checkbox2.isChecked();
		Boolean isCheckedDiaQuarta = checkbox3.isChecked();
		Boolean isCheckedDiaQuinta = checkbox4.isChecked();
		Boolean isCheckedDiaSexta = checkbox5.isChecked();
		Boolean isCheckedDiaSabado = checkbox6.isChecked();
		Boolean isCheckedDiaDomingo = checkbox7.isChecked();
		
		if(isCheckedDiaSegunda){
			rota.put("dias", "Segunda");
		}
		if(isCheckedDiaTerca){
			rota.put("dias", "Terca");
		}
		if(isCheckedDiaQuarta){
			rota.put("dias", "Quarta");
		}
		if(isCheckedDiaQuinta){
			rota.put("dias", "Quinta");
		}
		if(isCheckedDiaSexta){
			rota.put("dias", "Sexta");
		}
		if(isCheckedDiaSabado){
			rota.put("dias", "Sabado");
		}
		if(isCheckedDiaDomingo){
			rota.put("dias", "Domingo");
		}
		rota.put("idUser", Constants.userLogged.getObjectId());
		
		rota.saveInBackground(saveListener);
	}
	
	public void savePoints(String id) {
		ParseObject pontosRota = new ParseObject("PontosRota");
		
		PontosRota pontos = Constants.getPontosRota();
		pontos.parseLatLong();
		
		pontosRota.put("idRota", id);
		pontosRota.put("latitude", pontos.getLatitude());
		pontosRota.put("longitude", pontos.getLongitude());
		pontosRota.saveInBackground(savePointsListener);
	}
}
